package net.zjitc.controller;

import net.zjitc.pojo.AdminUser;
import net.zjitc.service.AdminService;
import net.zjitc.service.impl.AdminServiceImpl;
import net.zjitc.utils.dispacher.URLMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2017/11/19.
 */
public class AdminLogin {
    AdminService service = new AdminServiceImpl();
    @URLMapping(url = "/AdminloginUI")
    public String loginUI(HttpServletRequest request, HttpServletResponse response){
        return "/admin/index.jsp" ;
    }

    @URLMapping(url = "/Adminlogin")
    public String login(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        AdminUser admin =  service.findByUsernameAndPassword(username,password);
        if (admin == null){
            request.setAttribute("msg","用户名或密码错误！");
            return "/admin/index.jsp";
        }
        HttpSession session = request.getSession();
        session.setAttribute("admin",admin);
        String url = request.getContextPath()+"/admin/home.jsp";
        return "Redirect:"+url;
    }
}